/***********************************************************************
 * 
 * Copyright (c) 2009, Memo Akten, www.memo.tv
 *** The Mega Super Awesome Visuals Company ***
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 ***********************************************************************/

import TUIO.*;
TuioProcessing tuioClient;

final static float tuioCursorSpeedMult = 0.02f; // the iphone screen is so small, easy to rack up huge velocities! need to scale down 
final static float tuioStationaryForce = 0.001; // force exerted when cursor is stationary
final static float tuioDoubleTapDistanceThreshold2 = 0.01; // distance squared (in tuio units) of last two taps in order to count as doubletap
final static float tuioDoubleTapTimeThreshold = 0.2; // seconds between last two taps in order to count as doubletap

TuioPoint tuioLastTap;        // stores last tap information (to detect double tap)
boolean tuioDoubleTap = false;

void initTUIO() {
    tuioClient  = new TuioProcessing(this);
}


void updateTUIO() {
    Vector tuioCursorList = tuioClient.getTuioCursors();
    for (int i=0;i<tuioCursorList.size();i++) {
        TuioCursor tcur = (TuioCursor)tuioCursorList.elementAt(i);
        float vx = tcur.getXSpeed() * tuioCursorSpeedMult;
        float vy = tcur.getYSpeed() * tuioCursorSpeedMult;
        if(vx == 0 && vy == 0) {
            vx = random(-tuioStationaryForce, tuioStationaryForce);
            vy = random(-tuioStationaryForce, tuioStationaryForce);
        }
        addForce(tcur.getX(), tcur.getY(), vx, vy);
    }


    if(tuioDoubleTap) {
        drawFluid ^= true;
        tuioDoubleTap = false;
    }
}

void addTuioObject(TuioObject tobj) {
}

void updateTuioObject(TuioObject tobj) {
}

void removeTuioObject(TuioObject tobj) {
}



void addTuioCursor(TuioCursor tcur) {
    // check for double tap
    if(tuioLastTap != null) {  // only do this if we have a previous tap
    float timeMult = 0.000001;   // getTotalMilliseconds seems to be returning microseconds instead of milliseconds in current TUIO client library :/
        float nowTime = tcur.getTuioTime().getTotalMilliseconds() * timeMult;
        float lastTime = tuioLastTap.getTuioTime().getTotalMilliseconds() * timeMult;
//        println(nowTime + " - " + lastTime);
        if(nowTime - lastTime < tuioDoubleTapTimeThreshold) {    // check time different between current and previous tap
            float dx = tcur.getX() - tuioLastTap.getX(); // horizontal distance
            float dy = tcur.getY() - tuioLastTap.getY(); // vertical distance
            float d2 = dx * dx + dy * dy; // square of distance between taps
//            println(d2);
            if(d2 < tuioDoubleTapDistanceThreshold2) tuioDoubleTap = true; 
        }
    }

    tuioLastTap = new TuioPoint(tcur); // store info for next tap
}

void updateTuioCursor(TuioCursor tcur) {
}

void removeTuioCursor(TuioCursor tcur) {
}

void refresh(TuioTime bundleTime) {
}










